    //
//  AttendFamilySearchResultsViewController.m
//  BVCMS
//
//  Created by Allen Thomas on 11/13/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "AttendFamilySearchResultsViewController.h"
#import "AddNewPersonViewController.h"

@implementation AttendFamilySearchResultsViewController

@synthesize table;

- (void)viewDidLoad {
	UIBarButtonItem* addButton = [[UIBarButtonItem alloc] initWithTitle:@"Add New" style:UIBarButtonItemStylePlain target:self action:@selector(addNewPerson:)];
	self.navigationItem.rightBarButtonItem = addButton;
	[addButton release];	
}

- (void)addNewPerson:(id)sender {
	// Add a new person to Family
	AddNewPersonViewController *newPersonView = [[AddNewPersonViewController alloc]
												 initWithNibName:@"AddNewPersonView" bundle:[NSBundle mainBundle]];
	[self.navigationController pushViewController:newPersonView animated:YES];
	[newPersonView release];
}

/*
 Triggered when the user selects a table row.
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {	
	if( indexPath.row < 20 ) {
		selectedIndexPath = indexPath;
		[self popupActionSheet];
	}
}


-(void)popupActionSheet {
	UIActionSheet *popupQuery = [[UIActionSheet alloc]
								 initWithTitle:nil
								 delegate:self
								 cancelButtonTitle:@"Cancel"
								 destructiveButtonTitle:nil
								 otherButtonTitles:@"Add to Family",nil];
	
	popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
	[popupQuery showInView:self.tabBarController.view];
	[popupQuery release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex {
	
	if (buttonIndex == 0) {
		// Add a new person to Family
		AddNewPersonViewController *newPersonView = [[AddNewPersonViewController alloc]
													 initWithNibName:@"AddNewPersonView" bundle:[NSBundle mainBundle]];
		newPersonView.person = [self.resultsData objectAtIndex:[selectedIndexPath row]];
		[self.navigationController pushViewController:newPersonView animated:YES];
		[newPersonView release];
		
	}
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
	[self.table deselectRowAtIndexPath:selectedIndexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[table release];
    [super dealloc];
}


@end
